//
//  ZLPCollectionViewController.m
//  TangYi
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 赵连鹏. All rights reserved.
//

#import "ZLPCollectionViewController.h"
#import "ZLPTableViewModel.h"
#import "ZLPDataBaseHandle.h"
#import "ZLPMyTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "ZLPTopicViewController.h"


@interface ZLPCollectionViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSMutableArray *collectArr;

@end

@implementation ZLPCollectionViewController

- (void)dealloc {
    [_collectArr release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"专题收藏";
    
    [self createTableView];
    
    
    
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 14) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.collectArr.count;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [[self navigationController] popToRootViewControllerAnimated:YES];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuse = @"reuse";
    ZLPMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        
        cell = [[ZLPMyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    
    }
    
    ZLPTableViewModel *model = self.collectArr[indexPath.row];
    [cell.picView sd_setImageWithURL:[NSURL URLWithString:model.pic]];
    cell.titleLable.text = model.title;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZLPTopicViewController *topicVC = [[ZLPTopicViewController alloc] init];
    
    topicVC.numID = [self.collectArr[indexPath.row] numID];
    topicVC.topic = self.collectArr[indexPath.row];
    [self.navigationController pushViewController:topicVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

- (void)viewWillAppear:(BOOL)animated {
//    if (self.collectArr == nil) {
//       
//        
//    }
    
    self.tabBarController.tabBar.hidden = YES;
    
    self.collectArr = [NSMutableArray array];
    for (ZLPTableViewModel *temp in [[ZLPDataBaseHandle shareDataBaseHandle] selectAllTopic]) {
        [self.collectArr addObject:temp];
    }
    
    [self.tableView reloadData];
    
    if (self.collectArr.count == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您还没有收藏过物品,快去收藏吧~" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
